package com.laiat.location.activity;

import oauth.signpost.OAuth;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import com.laiat.location.R;

public class LocationListActivity extends Activity {
	
	private Menu optionsMenu;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.locations);
        
        if (!hasOAuthTokens()) {
        
			Intent intent = new Intent();
			intent.setClass(getApplicationContext(), SignInActivity.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			startActivity(intent);			
        }

    }
    
    private boolean hasOAuthTokens() {
    	SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    	
    	String token = prefs.getString(OAuth.OAUTH_TOKEN, null);
		String tokenSecret = prefs.getString(OAuth.OAUTH_TOKEN_SECRET, null);
		
		return (token != null & tokenSecret != null) ? true : false;
    }
    
	// Called first time user clicks on the menu button
    @Override
	public boolean onCreateOptionsMenu(Menu menu) {
		optionsMenu = menu;
    	MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
    	
    	return true;
	}
    
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
			case R.id.menuItemPrefs:
				startActivity(
						new Intent(this, PrefsActivity.class).addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT));
			break;
		}
		
		return true;
	}

    
	public Menu getOptionsMenu() {
		return optionsMenu;
	}
}